package org.jdsm.flow.gxt.users.client.widgets;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.core.client.util.Format;
import com.sencha.gxt.data.shared.loader.PagingLoadConfig;
import com.sencha.gxt.data.shared.loader.PagingLoader;
import com.sencha.gxt.widget.core.client.Dialog;
import com.sencha.gxt.widget.core.client.TabItemConfig;
import com.sencha.gxt.widget.core.client.TabPanel;
import com.sencha.gxt.widget.core.client.box.ConfirmMessageBox;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.event.BeforeStartEditEvent;
import com.sencha.gxt.widget.core.client.event.CellDoubleClickEvent;
import com.sencha.gxt.widget.core.client.event.HideEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.grid.Grid;
import com.sencha.gxt.widget.core.client.grid.editing.GridRowEditing;
import com.sencha.gxt.widget.core.client.info.Info;
import com.sencha.gxt.widget.core.client.selection.SelectionChangedEvent;
import com.sencha.gxt.widget.core.client.toolbar.PagingToolBar;
import com.sencha.gxt.widget.core.client.toolbar.ToolBar;
import org.jdsm.flow.gxt.users.client.UserAutoBeanFactory;
import org.jdsm.flow.gxt.users.client.widgets.role.RoleUserGrid;
import org.jdsm.flow.gxt.users.client.widgets.role.RolesGrid;

/**
 * User: mikom
 * Date: 12/6/12
 * Time: 6:00 PM
 */
public class RoleLayoutWidget implements IsWidget {

    private RolesGrid rolesGrid;
    private RoleUserGrid roleUserGrid;

    private UserAutoBeanFactory.Role selectedRole = null;

    private String LIST_OF_ROLES = "Roles";
    private String USERS_FOR_ROLE = "Users";


    public RoleLayoutWidget(String rolesPath, String userFullPath, String userCurrentPath, String saveOrUpdateRolePath, String saveRoleUsersPath) {
        UserAutoBeanFactory factory = GWT.create(UserAutoBeanFactory.class);
        rolesGrid = new RolesGrid(rolesPath, saveOrUpdateRolePath);
        roleUserGrid = new RoleUserGrid(factory, userFullPath, userCurrentPath, saveRoleUsersPath);

    }

    private void buildRolesTab(TabPanel advanced) {
        final PagingToolBar pagingToolBar = new PagingToolBar(5);
        VerticalLayoutContainer cont = new VerticalLayoutContainer();


        ToolBar toolBar = new ToolBar();
        TextButton addBtn = new TextButton("add");
        final GridRowEditing<UserAutoBeanFactory.Role> editing = new GridRowEditing<UserAutoBeanFactory.Role>(rolesGrid.getGrid());
        editing.startEditing(new Grid.GridCell(0, 0));


        addBtn.addSelectHandler(new SelectEvent.SelectHandler() {
            @Override
            public void onSelect(SelectEvent event) {
                UserAutoBeanFactory factory = GWT.create(UserAutoBeanFactory.class);
                UserAutoBeanFactory.Role addRole = factory.create(UserAutoBeanFactory.Role.class).as();
                addRole.setName("new role name");
                addRole.setPermission(Integer.MAX_VALUE);
                rolesGrid.getGrid().getStore().add(0, addRole);
            }
        });


        toolBar.add(addBtn);

        cont.add(toolBar);
        cont.add(rolesGrid, new VerticalLayoutContainer.VerticalLayoutData(1, 1));
        cont.add(pagingToolBar, new VerticalLayoutContainer.VerticalLayoutData(1, -1));

        PagingLoader<? extends PagingLoadConfig, ?> loader = rolesGrid.getLoader();
        pagingToolBar.bind(loader);
        loader.load();
        advanced.add(cont, LIST_OF_ROLES);
    }

    private void buildRoleUsersTab(TabPanel advanced, TabItemConfig config) {
        config.setEnabled(false);
        ToolBar toolBar = new ToolBar();
        VerticalLayoutContainer container = new VerticalLayoutContainer();
        container.setBorders(true);
        container.add(toolBar);

        TextButton saveBtn = new TextButton("save");
        saveBtn.addSelectHandler(new SelectEvent.SelectHandler() {
            @Override
            public void onSelect(SelectEvent selectEvent) {
                roleUserGrid.saveData();
                roleUserGrid.saveAction(selectedRole);
            }
        });
        toolBar.add(saveBtn);
        container.add(roleUserGrid);
        advanced.add(container, config);

    }


    @Override
    public Widget asWidget() {
        VerticalPanel vp = new VerticalPanel();
        vp.setSpacing(10);

        final TabPanel advanced = new TabPanel();
        advanced.setPixelSize(600, 350);
        advanced.setAnimScroll(true);
        advanced.setTabScroll(true);
        advanced.setCloseContextMenu(true);


        buildRolesTab(advanced);

        final TabItemConfig tabItemConfigForUsers = new TabItemConfig(USERS_FOR_ROLE);
        buildRoleUsersTab(advanced, tabItemConfigForUsers);

        advanced.addSelectionHandler(new SelectionHandler<Widget>() {
            @Override
            public void onSelection(SelectionEvent<Widget> event) {
                TabPanel panel = (TabPanel) event.getSource();
                Widget w = event.getSelectedItem();
                TabItemConfig config = panel.getConfig(w);
                String tabTitle = config.getText();
                if (tabTitle == null) {
                    return;
                }
                if (tabTitle.equals(USERS_FOR_ROLE)) {
                    roleUserGrid.loadDataByRole(selectedRole);
                }

            }
        });
        rolesGrid.getGrid().getSelectionModel().addSelectionChangedHandler(new SelectionChangedEvent.SelectionChangedHandler<UserAutoBeanFactory.Role>() {
            @Override
            public void onSelectionChanged(SelectionChangedEvent<UserAutoBeanFactory.Role> userSelectionChangedEvent) {
                if (userSelectionChangedEvent.getSelection().size() == 1) {
                    selectedRole = rolesGrid.getGrid().getStore().findModel(userSelectionChangedEvent.getSelection().get(0));
                    tabItemConfigForUsers.setEnabled(true);
                    advanced.forceLayout();
                } else {
                    rolesGrid.getGrid().getSelectionModel().deselect(selectedRole);
                    selectedRole = rolesGrid.getGrid().getSelectionModel().getSelectedItem();
                }

                if (selectedRole.getId() == null) {
                    tabItemConfigForUsers.setEnabled(false);
                }
            }
        });

        vp.add(advanced);

        return vp;
    }
}
